home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
program
/
4th86_v4.zip
/
ARRAY.4TH
< prev
next >
Wrap
Text File
|
1994-01-01
|
6KB
|
191 lines
( forget strt
: strt ; )
off printload
unsplit
( **************************************************** )
( * this file contains an example of a defining word * )
( * * )
( * source extended from the original CP/M80 * )
( **************************************************** )
(
* Most FORTH words execute some function -- such as drawing a line;
* storing a variable in memory; saving a file etc.
*
* COMPILER WORDS are different in that their effect is to extend the
* scope of the FORTH interpreter itself by creating ( or part creating )
* a new word.
*
* The existing words CODE; CONSTANT; BLOCK; -- and of course the colon
* and semicolon that start and end all definitions -- are compiler words.
*
* What this file does is illustrate how to write your own compiler words.
* The word chosen is ARRAY -- which functions in much the same manner as
* BLOCK.
*
* 2 BLOCK FRED for example creates a storage are of 2 bytes which
* is accessible by the name FRED.
*
* 3 4 ARRAY KEN will be a word which creates a storage area of
* ( 3 x 4 ) = 12 bytes-pairs [ ie 16 bits ] -- and allows cells to be
* read again by a command such as 2 3 KEN @
*
* The defined word, when executed, returns the address
* of specified cell (tos is row, nos is column)
* Each cell is 16 bits. For example,
*
* 8 3 ARRAY XYZ ( 3 rows (0 to 2), 8 columns (0 to 7))
* 0 4 0 XYZ ! ( store 0 in 0th row, 4th column)
* 7 2 XYZ @ ( fetch last cell in array)
*
* No error checking at compile time or run time, add it if needed.
*
*
* ARRAY designed to build the following code:
* CALL xarray ;call runtime, tos will have adr of dw's
* DW base ;base of data space allocated
* DW #rows-1
* DW #cols-1
* )
: ARRAY ( col row -- word)
0 DEFINE ( put name in dictionary)
e8H HEADB! ( opcode for CALL )
' XARRAY ( XARRAY not defined yet - no problem though as
in high mode, tick (') does not try to look up
the address of following word. Instead, it
stores text string "XARRAY" in-line so word can
be looked up ( in the appropriate dictionary ) when
ARRAY executes )
head @ 2+ - HEAD! ( relative call address of XARRAY )
OVER OVER * 2 * LUM @ SWAP - DUP LUM ! ( alloc memory)
HEAD! ( store base address after call)
HEAD! ( store number rows) ( for later error checking)
2 * HEAD! ( store # bytes/row )
; IMMEDIATE
( ---------------------------------------------------- )
( run time for ARRAY)
(
* this is comparable to the exiting words VLOAD, LVLOAD etc. It
* extracts the address pointer to the data from the array.
)
: XARRAY ( col row -- adr)
SWAP OVER 4 + @ ( get 'column*2' dimension)
* ( offset to correct row)
SWAP @ ( get array base adr) + ( baseadr + rowofset)
SWAP DUP + ( column offset) + ( got adr of cell)
;
( **** NOTE ***
* If you are creating a standalone turnkey COM file that uses the word
* ARRAY - then it must be pre-loaded into the "mother" system you are
* using to cross-compile the standalone COM file.
*
* While it is **NOT** needed in the actual standalone COM file ( Only the
* routine XARRAY is needed there ) it will do no harm to include it.
*
* You will need XARRAY in the mother system as well as ARRAY to be able
* to use ARRAY during [ non cross compile ] development and debugging.
)
( ---------------------------------------------------- )
on printload
cls
( Following is an illustration of the use of ARRAY
The word SHOWCALENDAR is used to display dates against days.
It takes two parameters -- X Y SHOWCALENDAR
where X is a number from 0 through 6 to represent the starting day
Y is a number from 28 through 31 to represent the days in the month
so 4 30 SHOWCALENDAR will display 30 days starting on Wednesday )
off printload
( ** NOTE ** there is no check on the values of X and Y inserted. Impossible
values such as days > 31 or starting day > 6 will be accepted. Modify as you
wish to check the input parameters for valid range )
: HEADER crlf " Su Mo Tu We Th Fr Sa " ." crlf
" ============================ " ." ;
7 5 array calendar
2 block startday
2 block maxdate
( ***********
fill array with numbers 0 through maxdate
fill actually starts with a negative value ( 0 - startdate )
and where a negative value would have been inserted, 40 is inserted
instead. [ 40 is arbitrary - any number greater than 31 will do ]
40 is treated specially in SHOWC which follows
*********** )
: fillc 0 startday @ -
4 0 ( rows )
do 6 0 ( columns )
│ do 1+ dup
│ │ dup 1 < ( previous month )
│ │ if
│ │ │ drop 40 ( so put in a 40 )
│ │ then
│ │ i j calendar !
│ loop
loop drop ;
( ***********
fill array with
arbitrary value 40
*********** )
: clear 4 0
do 6 0
│ do
│ │ 40 i j calendar !
│ loop
loop ;
( ***********
display array on screen
*********** )
: showc 4 0 ( rows )
do crlf 6 0 ( columns )
│ do i j calendar @ dup 10 <
│ │ if
│ │ │ 1 spcs ( extra leading space for single digits )
│ │ then dup maxdate @ >
│ │ if
│ │ │ 2 spcs drop 0 .c 1 spcs ( print blank space character )
│ │ else
│ │ │ . 1 spcs ( print valid date )
│ │ then
│ loop
loop ;
( ***********
main routine
*********** )
: showcalendar maxdate ! startday !
clear header fillc showc ;
" __________________________________________________________ " ." crlf crlf
" 3 30 showcalendar " ."
3 30 showcalendar crlf crlf crlf